package com.huiminpay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.RandomUuidUtil;
import com.huiminpay.common.cache.util.StringUtil;
import com.huiminpay.merchant.api.AppService;
import com.huiminpay.merchant.convert.AppConvert;
import com.huiminpay.merchant.dto.AppDTO;
import com.huiminpay.merchant.entity.App;
import com.huiminpay.merchant.entity.Merchant;
import com.huiminpay.merchant.mapper.AppMapper;
import com.huiminpay.merchant.mapper.MerchantMapper;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: yz
 * @Date: 2024/7/23 11:48
 */
@Service //发布dubbo服务
public class AppServiceImpl implements AppService {
    @Resource
    private AppMapper appMapper;

    @Resource
    private MerchantMapper merchantMapper;

    /**
     * 创建应用
     *
     * @param merchantId 商户id
     * @param app        应用信息
     * @return
     * @throws BusinessException
     */
    @Override
    public AppDTO createApp(Long merchantId, AppDTO app) throws BusinessException {
        if (merchantId == null || app == null || StringUtil.isBlank(app.getAppName())) {
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant == null) {
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        if (!"2".equals(merchant.getAuditStatus())) {
            throw new BusinessException(CommonErrorCode.E_200003); //商户未审核通过

        }
        //name唯一性校验
        if (isExistAppName(app.getAppName())) {
            throw new BusinessException(CommonErrorCode.E_200004); //应用名称已存在
        }
        //dto转entity
        App entity = AppConvert.INSTANCE.DTO2Entity(app);
        entity.setMerchantId(merchantId);
        entity.setAppId(RandomUuidUtil.getUUID());
        appMapper.insert(entity);


        return AppConvert.INSTANCE.entity2DTO(entity);
    }

    /**
     * 判断应用名是否已存在
     *
     * @param appName
     * @return
     */
    private boolean isExistAppName(String appName) {
        Integer count = appMapper.selectCount(new QueryWrapper<App>().lambda().eq(App::getAppName, appName));
        return count > 0;
    }

    /**
     * 查询商品下所有应用
     *
     * @param merchantId
     * @return 应用列表
     * @throws BusinessException
     */
    @Override
    public List<AppDTO> queryAppsByMerchantId(Long merchantId) throws BusinessException {
        if (merchantId == null) {
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        List<App> apps = appMapper.selectList(new QueryWrapper<App>().lambda().eq(App::getMerchantId, merchantId));
        return AppConvert.INSTANCE.entityList2DTOList(apps);

    }

    /**
     * 根据appId查询应用信息
     *
     * @param appId
     * @return
     * @throws BusinessException
     */
    @Override
    public AppDTO queryAppById(String appId) throws BusinessException {
        if (appId == null) {
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        App app = appMapper.selectOne(new QueryWrapper<App>().lambda().eq(App::getAppId, appId));
        return AppConvert.INSTANCE.entity2DTO(app);
    }

    /**
     * 查询应用是否属于某个商户
     *
     * @param appId      应用id
     * @param merchantId 商户id
     * @return 属于商户返回true，否则false
     */
    @Override
    public Boolean queryAppInMerchant(String appId, Long merchantId) {
        // 创建LambdaQueryWrapper查询对象
        LambdaQueryWrapper<App> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件，根据商户id和应用id查询
        queryWrapper.eq(App::getMerchantId, merchantId)
                .eq(App::getAppId, appId);

        // 执行查询，获取符合条件的记录数
        Integer count = appMapper.selectCount(queryWrapper);

        // 判断记录数是否大于0，若大于0则表示属于该商户，返回true；否则返回false
        return count > 0;
    }

}
